[#ftl]
[#import "common/macrolibs.ftl" as lib]
[#import "common/commonclass.ftl" as cls]
package ${lib.packageBase}.actions;

import java.util.Map;
[#if (clazz.mtmRelations?size>0)]
import org.apache.commons.lang.StringUtils;
[/#if]
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
[#if ((clazz.scopeClass??)||(clazz.mtmRelations?size>0))]
import org.springframework.web.bind.annotation.PathVariable;
[/#if]
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
[#if (clazz.mtmRelations?size>0)]
import org.springframework.web.bind.annotation.RequestParam;
[/#if]
import org.springframework.web.bind.annotation.ResponseBody;

import com.smzd.core.business.IService;
import com.smzd.core.utils.db.${cls.myPageName};
[#if (clazz.mtmRelations?size>0)]
import com.smzd.core.utils.json.JsonHelper;
[/#if]
import com.smzd.core.actions.${cls.myControllerExtends};
[@lib.importOwnerClass level=clazz.ownerLevel/]

import ${lib.packageBase}.business.${cls.mySimpleClassName}Service;
import ${lib.packageBase}.domain.${cls.mySimpleClassName};
[#if clazz.scopeClass??]
import ${lib.packageBase}.business.${cls.myScopeClassName}Service;
import ${lib.packageBase}.domain.${cls.myScopeClassName};
[/#if]

@Controller
@RequestMapping("[#if clazz.scopeClass??]/${cls.myScopeModuleName}/{${cls.myScopePathVariable}}[/#if]/${cls.myModuleNames}")
public class ${cls.mySimpleClassName}Controller extends ${cls.myControllerExtends}<${cls.mySimpleClassName}[#if clazz.scopeClass??], ${cls.myScopeClassName}[/#if]> {
    @Autowired
    private ${cls.mySimpleClassName}Service service;

    @Override
    protected IService<${cls.mySimpleClassName}> getService() {
        return service;
    }
[#if clazz.scopeClass??]
    @Autowired
    private ${cls.myScopeClassName}Service ${cls.myScopeModuleName}Service;
    
    @Override
    protected ${cls.myScopeClassName} getScope(int scopeId) {
        return ${cls.myScopeModuleName}Service.getItemById(scopeId);
    }
[/#if]

    @RequestMapping(value="/add", method=RequestMethod.GET)
    @Override
    public String toAddPage(Map<String, Object> model,[#if clazz.scopeClass??]
            ${cls.myScopeIdPathVar}[/#if] ${cls.mySimpleClassName} elem,
            BindingResult result) throws Exception {
        return super.toAddPage(model,[#if clazz.scopeClass??] scopeId,[/#if] elem, result);
    }

    @RequestMapping(value="/add", method=RequestMethod.POST)
    @ResponseBody
    @Override
    public String postAddData(Map<String, Object> model,[#if clazz.scopeClass??]
            ${cls.myScopeIdPathVar}[/#if] ${cls.mySimpleClassName} elem,
            BindingResult result) throws Exception {
        return super.postAddData(model,[#if clazz.scopeClass??] scopeId,[/#if] elem, result);
    }

    @RequestMapping(method=RequestMethod.GET)
    @Override
    public String toListPage(Map<String, Object> model,[#if clazz.scopeClass??]
            ${cls.myScopeIdPathVar}[/#if] ${cls.mySimpleClassName} elem,
            BindingResult result) throws Exception {
        return super.toListPage(model,[#if clazz.scopeClass??] scopeId,[/#if] elem, result);
    }

    @RequestMapping(value="/list", method=RequestMethod.POST)
    @ResponseBody
    @Override
    public String getListData(Map<String, Object> model,[#if clazz.scopeClass??]
            ${cls.myScopeIdPathVar}
            [/#if]${cls.myPageName}${cls.myPageParam} page,
            BindingResult result) throws Exception {
        return super.getListData(model,[#if clazz.scopeClass??] scopeId,[/#if] page, result);
    }

    @RequestMapping(value="/edit", method=RequestMethod.GET)
    @Override
    public String toEditPage(Map<String, Object> model,[#if clazz.scopeClass??]
            ${cls.myScopeIdPathVar}[/#if] ${cls.mySimpleClassName} elem,
            BindingResult result) throws Exception {
        elem = service.getItemById(elem.getId());
        return super.toEditPage(model,[#if clazz.scopeClass??] scopeId,[/#if] elem, result);
    }

    @RequestMapping(value="/edit", method=RequestMethod.POST)
    @ResponseBody
    @Override
    public String postEditData(Map<String, Object> model,[#if clazz.scopeClass??]
            ${cls.myScopeIdPathVar}[/#if] ${cls.mySimpleClassName} elem,
            BindingResult result) throws Exception {
        return super.postEditData(model,[#if clazz.scopeClass??] scopeId,[/#if] elem, result);
    }

    @RequestMapping(value="/remove", method=RequestMethod.POST)
    @ResponseBody
    @Override
    public String postRemoveData(Map<String, Object> model,[#if clazz.scopeClass??]
            ${cls.myScopeIdPathVar}[/#if] ${cls.mySimpleClassName} elem,
            BindingResult result) throws Exception {
        return super.postRemoveData(model,[#if clazz.scopeClass??] scopeId,[/#if] elem, result);
    }
    
    [#if (clazz.mtmRelations?size>0)]
    [#list clazz.mtmRelations as mtm]
    [#assign masterClassName][@lib.simpleClassName clazz=mtm.master/][/#assign]
    [#assign slaveClassName][@lib.simpleClassName clazz=mtm.slave/][/#assign]
    [#assign configView][@lib.moduleName clazz=mtm.master/][@lib.moduleNames clazz=mtm.slave/].ftl[/#assign]
    [#assign masterId]${masterClassName?uncap_first}Id[/#assign]
    [#assign masterIdPath]${masterClassName?lower_case}[/#assign]
    ///////////////扩展功能--${mtm.master.name}与${mtm.slave.name}的多对多编辑////////////////////
    /**
     * to${masterClassName}${slaveClassName}Page：导航到${masterClassName}-${slaveClassName}的编辑页面。<br>
     * @param model
     * @param ${masterId}
     * @return
     */
    @RequestMapping(value="/${masterIdPath}-{${masterId}}", method=RequestMethod.GET)
    public String to${masterClassName}${slaveClassName}Page(Map<String, Object> model, 
        @PathVariable("${masterId}") int ${masterId}){
        model.put("${masterId}", ${masterId});
        return getMyView("${configView}");
    }
    /**
     * getOuter${slaveClassName}ListBy${masterClassName}：获取${masterClassName}-${slaveClassName}配置列表。<br>
     * 目标${masterClassName}已配置的${slaveClassName}被排在前面，并且以文本/颜色标记<br>
    * @param model
     * @param ${masterId}
     * @param page
     * @param result
     * @return
     */
    @RequestMapping(value="/outer/${masterIdPath}-{${masterId}}", method=RequestMethod.POST)
    @ResponseBody
    public String getOuter${slaveClassName}ListBy${masterClassName}(Map<String, Object> model,
            [#if clazz.scopeClass??]${cls.myScopeIdPathVar}
            [/#if]@PathVariable("${masterId}") int ${masterId},
            ${cls.myPageName}${cls.myPageParam} page,
            BindingResult result){
        page.setExt(String.valueOf(${masterId}));
        [#if clazz.scopeClass??]
        page.setScope(getScope("${cls.myScopePathVariable}"));
        [/#if]
        return service.getOuter${slaveClassName}ListBy${masterClassName}(page);
    }
    /**
     * save${masterClassName}${slaveClassName}Relations：保存${masterClassName}-${slaveClassName}的改变情况。<br>
     * 只有新改变的部分数据需要提交服务器端进行处理。<br>
     * @param model
     * @param ${masterId}
     * @param ids
     * @return
     * @throws JsonRequestException
     */
    @RequestMapping(value="/edit/${masterIdPath}-{${masterId}}", method = RequestMethod.POST)
    @ResponseBody
    public String save${masterClassName}${slaveClassName}Relations(Map<String, Object> model,
            @PathVariable("${masterId}") int ${masterId}, 
            @RequestParam("ids") String ids)
            throws Exception {
        try {
            if(StringUtils.isEmpty(ids)){
                return JsonHelper.getSuccessJsonString("没有任何变动提交！");
            }
            service.save${masterClassName}${slaveClassName}Relations(${masterId}, ids);
            return JsonHelper.getSuccessJsonString("保存成功！");
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
    [/#list]
    [/#if]
}